var BillMaterialFormModel = function (order, material) {
  BaseModel.call(this);

  var self = this;
  var warehouseList = [{
    value: "1",
    label: "原材料仓"
  }, {
    value: "2",
    label: "辅料仓"
  }];
  var changeFields = ['materialName', 'model', 'color', 'providerName'];

  var fn = {
    initData: function (order, material) {
      this.resetData(order);
      if (material === null) {
        self.saveOldData();
        return;
      }

      var fields = ['color', 'colorDescription', 'materialName', 'materialType', 'providerNo'];
      self.setDataFromObj(material, fields, '', true);

      var model = material.materialModel;
      if (SysUtil.isEmpty(model)) {
        model = '';
      }
      self.model = model.trim();
      self.feedTotal = material.feedTotal;
      self.yltsTotal = material.yltsTotal;
      self.orderTotal = material.orderTotal;

      var materialFactory = material.materialFactory;
      self.defaultWarehouse = this.getWarehouse(materialFactory, 'label');

      var materialDept = material.materialDept;
      if (SysUtil.isEmpty(materialDept)) {
        materialDept = '';
      }
      self.materialDept = materialDept;

      this.resetIsEquals();
    },

    resetData: function (order) {
      var fields = ['materialType', 'colorDescription', 'materialCode', 'unit',
        'material', 'transmittance', 'isEquals', 'providerNo'
      ];
      fields.pushArray(changeFields);
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        self[field] = '';
      }

      self.feedTotal = undefined;
      self.yltsTotal = undefined;
      self.unitPrice = undefined;
      self.procurementCycles = undefined;
      self.orderTotal = undefined;
      self.materialDept = '';

      self.setDataFromObj(order, ['defaultWarehouse']);
      self.orderNo = order.orderId;
    },

    resetIsEquals: function () {
      var unit = self.unit;
      var isEquals;
      if (unit === '付') {
        isEquals = 1;
      } else {
        isEquals = 0;
      }
      self.isEquals = isEquals;
    },

    getWarehouse: function (data, field) {
      var dataField = field === 'value' ? 'label' : 'value';
      var warehouse = _.find(warehouseList, function (obj) {
        return obj[dataField] === data;
      });
      if (warehouse === undefined) {
        return '';
      }
      return warehouse[field];
    },

    getErrorMsg: function (code) {
      var errorObj = {
        materialName: "请输入物料名称",
        model: "请输入规格型号",
        providerName: "请选择供应商名称",
        materialType: "请选择物料类型",
        feedTotal: "请输入投料数",
        unit: "请选择单位",
        unitPrice: "请输入单价",
        procurementCycles: "请输入采购周期"
      };
      return errorObj[code];
    },

    resetMaterialDept: function (material) {
      var materialDept = material.materialDept;
      if (materialDept !== undefined) {
        if (SysUtil.isEmpty(materialDept)) {
          materialDept = '';
        }
        self.materialDept = materialDept;
      }
    }
  };

  this.saveOldData = function () {
    this.setOldData(changeFields);
  };

  this.isChange = function () {
    for (var i = 0; i < changeFields.length; i++) {
      var field = changeFields[i];
      var oldValue = this.getOldData(field);
      var value = _.get(this, field);
      if (oldValue !== value) {
        return true;
      }
    }
    return false;
  };

  this.resetObject = function (data) {
    var fields = ['materialCode', 'unit', 'material', 'unitPrice', 'procurementCycles', 'transmittance', 'providerNo'];
    fields.pushArray(changeFields);
    this.setDataFromObj(data, fields);
    fn.resetIsEquals();
    fn.resetMaterialDept(data);

    var materialType = data.materialType;
    if (!SysUtil.isEmpty(materialType)) {
      this.materialType = materialType;
    }
  };

  this.getSearchObject = function () {
    var postData = {};
    this.setPostData(postData, changeFields, true, true);
    if (postData.materialName === '' || postData.model === '' || postData.providerName === '') {
      return null;
    }
    return postData;
  };

  this.checkValid = function (canUpdate) {
    var fields = ['materialName', 'model', 'providerName', 'materialType', 'feedTotal'];
    if (!CheckVaildUtil.checkNullValue(fields, this, fn.getErrorMsg)) {
      return false;
    }

    if (canUpdate) {
      var feedFields = ['unit', 'unitPrice', 'procurementCycles']
      if (!CheckVaildUtil.checkNullValue(feedFields, this, fn.getErrorMsg)) {
        return false;
      }
    }
    return true;
  };

  this.getObject = function (canUpdate) {
    var postData = {};
    var orderInfoDetail = {
      materialModel: this.model,
      materialFactory: fn.getWarehouse(this.defaultWarehouse, 'value'),
      feedTotal: this.feedTotal + '',
      yltsTotal: this.yltsTotal === undefined ? null : this.yltsTotal + '',
      orderTotal: this.orderTotal === undefined ? null : this.orderTotal + '',
    };
    var orderFileds = ['orderNo', 'materialType', 'color', 'colorDescription', 'providerNo'];
    this.setPostData(orderInfoDetail, orderFileds, true, true);

    if (canUpdate) {
      var materialData = {};
      var materialFields = ['materialName', 'materialType', 'model', 'color', 'colorDescription', 'providerName',
        'unit', 'unitPrice', 'defaultWarehouse', 'isEquals', 'procurementCycles', 'material', 'providerNo'
      ];
      if (this.materialType === '镜片') {
        materialFields.push('transmittance');
      }
      this.setPostData(materialData, materialFields, true, true);
      var materialDept = this.materialDept;
      if (materialDept !== '') {
        materialData.materialDeptId = materialDept.materialDeptId;
      }
      postData.materialData = JSON.stringify(materialData);
    } else {
      orderInfoDetail.materialCode = this.materialCode;
    }
    postData.orderInfoDetail = JSON.stringify(orderInfoDetail);
    return postData;
  };

  fn.initData(order, material);
}
